package com.smartresume.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * SmartResume-AI 后端应用启动类
 * 
 * @author SmartResume Team
 * @version 1.0.0
 */
@SpringBootApplication
public class SmartResumeApplication {

    public static void main(String[] args) {
        SpringApplication.run(SmartResumeApplication.class, args);
        System.out.println("=================================");
        System.out.println("SmartResume-AI 后端服务启动成功！");
        System.out.println("服务地址: http://localhost:8080");
        System.out.println("API文档: http://localhost:8080/api/resume/analyze");
        System.out.println("=================================");
    }

    /**
     * 配置RestTemplate Bean，用于调用AI服务
     */
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        
        // 设置UTF-8编码支持
        restTemplate.getMessageConverters().clear();
        
        // 添加UTF-8字符串转换器
        org.springframework.http.converter.StringHttpMessageConverter stringConverter = 
            new org.springframework.http.converter.StringHttpMessageConverter(java.nio.charset.StandardCharsets.UTF_8);
        stringConverter.setWriteAcceptCharset(false);
        restTemplate.getMessageConverters().add(stringConverter);
        
        // 添加JSON转换器
        org.springframework.http.converter.json.MappingJackson2HttpMessageConverter jsonConverter = 
            new org.springframework.http.converter.json.MappingJackson2HttpMessageConverter();
        jsonConverter.setDefaultCharset(java.nio.charset.StandardCharsets.UTF_8);
        restTemplate.getMessageConverters().add(jsonConverter);
        
        return restTemplate;
    }

    /**
     * 配置CORS跨域支持
     */
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/**")
                        .allowedOrigins("http://localhost:5173", "http://localhost:3000")
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                        .allowedHeaders("*")
                        .allowCredentials(true);
            }
        };
    }
}